package com.example.springbootvue.Controller;

import com.example.springbootvue.Pojo.Article;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/articles")
@CrossOrigin
public class ArticleController {
    private List<Article> articleList = new ArrayList<>();

    {
        articleList.add(new Article("Article 1", "时间 ", "2222-01-01", "发布"));
        articleList.add(new Article("Article 2", "Article 2", "2222-02-01", " description"));
        articleList.add(new Article("Article 3", "Article 3", "2222-03-01", " description"));
        articleList.add(new Article("Article 4", "Article 4", "2222-04-01", " description"));
        articleList.add(new Article("Article 5", "Article 5", "2222-05-01", " description"));
        articleList.add(new Article("Article 6", "Article 6", "2222-06-01", " description"));
        articleList.add(new Article("Article 7", "Article 7", "2222-07-01", " description"));
        articleList.add(new Article("Article 8", "Article 8", "2222-08-01", " description"));
    }

    @PostMapping("/add")
    public String addArticle(@RequestBody Article article) {
        articleList.add(article);
        return "Article added successfully";
    }

    @GetMapping("/getall")
    public List<Article> getAllArticles() {
        return articleList;
    }

    @GetMapping("/search")
    public List<Article> search( String category,String state) {
        return  articleList.stream().filter(article -> article.getCategory().equals(category) && article.getState().equals(state)).collect(Collectors.toList());
    }
}

